package com.ply.flashsalessystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ply.flashsalessystem.annotation.AuthorityName;
import com.ply.flashsalessystem.annotation.StoreAuthority;
import com.ply.flashsalessystem.annotation.UserAuthority;
import com.ply.flashsalessystem.entity.pojo.Tag;
import com.ply.flashsalessystem.entity.result.Result;
import com.ply.flashsalessystem.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaoPan
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/tag")
@Api("tag 接口")
@CrossOrigin
public class TagController {
    // 增删改查
    @Autowired
    TagService tagService;

    @AuthorityName(authorityId = 59, authorityName = "查询所有商品类型")
    @UserAuthority
    @StoreAuthority
    @GetMapping("getTag")
    public Result getTag() {
        List<Tag> list = tagService.list();
        return Result.ok().message("查询tag成功").data("rows", list);
    }

    @AuthorityName(authorityId = 60, authorityName = "管理员 增加tag")
    @ApiOperation("增加tag")
    @PostMapping("addTag")
    public Result addTag(@RequestBody Tag tag) {
        String name = tag.getTagName();
        if (name == null){
            throw new RuntimeException("类型 不可以为空");
        }

        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_name", name);
        Tag one = tagService.getOne(queryWrapper);
        if (one != null) {
            return Result.error().message("已经有该分类了");
        }
        Tag tag1 = new Tag();
        tag1.setTagName(name);
        return tagService.save(tag1)
                ? Result.ok().message("增加成功")
                : Result.error().message("增加失败");
    }
}

